package com.yonyougov.devops.cruxcisupport.menu;

import com.yonyougov.devops.cruxcisupport.button.ButtonEntity;
import com.yonyougov.devops.cruxcisupport.button.ButtonService;
import com.yonyougov.devops.cruxcisupport.menu.vo.AllMenuLinkRespVo;
import com.yonyougov.devops.cruxcisupport.menu.vo.NoLinkMenuByRoleIdVo;
import com.yonyougov.devops.cruxcisupport.menu.vo.NoLinkMenuPageByRoleIdDto;
import com.yonyougov.devops.cruxcisupport.rolemenu.RoleMenuEntity;
import com.yonyougov.devops.cruxcisupport.rolemenu.RoleMenuService;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

/**
 * t_menu服务实现
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<MenuEntity, BigInteger> implements MenuService {
    private final MenuDao menuDao;

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private ButtonService buttonService;

    public MenuServiceImpl(MenuDao menuDao) {
        this.menuDao = menuDao;
    }

    @Override
    public BaseDao<MenuEntity, BigInteger> getDao() {
        return menuDao;
    }


    //<!--用户自定义代码开始-->

    @Override
    public Pagination<NoLinkMenuByRoleIdVo> findNoLinkMenuPageByRoleId(NoLinkMenuPageByRoleIdDto noLinkMenuPageByRoleIdDto) {
        List<NoLinkMenuByRoleIdVo> noLinkMenuByRoleIdVoList = menuDao.findNoLinkMenuPageByRoleId(noLinkMenuPageByRoleIdDto);
        int totalCount = menuDao.countNoLinkMenuByRoleId(noLinkMenuPageByRoleIdDto);
        return new Pagination<NoLinkMenuByRoleIdVo>(noLinkMenuPageByRoleIdDto.getMax(), noLinkMenuPageByRoleIdDto.getPageNum(), totalCount, noLinkMenuByRoleIdVoList);
    }

    @Override
    public List<AllMenuLinkRespVo> findLinkMenuByUserId(BigInteger userId) {
        return menuDao.findAllMenuLinkByUserId(userId);
    }

    @Override
    public List<MenuEntity> findAllRootMenu() {
        MenuEntity menuEntity = new MenuEntity();
        menuEntity.setParentId(new BigInteger(String.valueOf(0)));
        menuEntity.setPageAble(false);
        Pagination<MenuEntity> allRootMenu = findAllBy(menuEntity);
        return allRootMenu.getDatas();
    }

    @Override
    public void deleteOneMenuByMenuId(BigInteger menuId) {
        // 删菜单
        deleteByPrimaryKey(menuId);
        // 删角色菜单联系
        RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
        roleMenuEntity.setMenuId(menuId);
        roleMenuService.delete(roleMenuEntity);
        // 删菜单下的按钮
        ButtonEntity buttonEntity = new ButtonEntity();
        buttonEntity.setMenuId(menuId);
        buttonService.delete(buttonEntity);
    }

    //<!--用户自定义代码结束-->
}
